<template>
  <div class="password">
    <login-heade>
      <el-form
        slot="container"
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-position="left"
        label-width="0px"
      >
        <div class="title">
          <h3>找回密码</h3>
        </div>
        <!-- username -->
        <el-form-item prop="username">
          <el-input
            type="text"
            v-model="ruleForm.username"
            auto-complete="off"
            placeholder="账号"
          >
            <i slot="prefix" class="fa fa-user-o"></i>
          </el-input>
        </el-form-item>

        <!-- email -->
        <el-form-item prop="email">
          <el-input
            type="text"
            v-model="ruleForm.email"
            auto-complete="off"
            placeholder="邮箱"
          >
            <i slot="prefix" class="fa fa-envelope-o"></i>
          </el-input>
        </el-form-item>

        <!-- 确定 -->
        <el-form-item>
          <el-button
            @click.native.prevent="handleSubmit"
            :loading="loading"
            type="primary"
            style="width: 100%"
            >确定</el-button
          >
        </el-form-item>
      </el-form>
    </login-heade>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Provide } from 'vue-property-decorator'
import LoginHeade from './LoginHeade.vue'
import { findPwd } from '../../api/user'

@Component({
  components: { LoginHeade },
})
export default class Password extends Vue {
  @Provide() loading: boolean = false //是否发送请求
  @Provide() ruleForm: {
    username: string
    email: string
  } = {
    username: '',
    email: '',
  }
  @Provide() rules = {
    username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
    email: [
      {
        required: true,
        message: '请输入邮箱地址',
        trigger: 'blur',
      },
      {
        //邮箱校验
        type: 'email',
        message: '请输入正确的邮箱地址',
        trigger: 'blur,change',
      },
    ],
  }
  handleSubmit(): void {
    let _this = this as any
    this.loading = true
    _this.$refs['ruleForm'].validate((valid: boolean) => {
      if (valid) {
        findPwd(_this.ruleForm)
          .then((res: any) => {
            this.loading = false
            console.log(res)
            _this.$message.success(res.data.msg)
            this.$router.push('/login')
          })
          .catch((err: any) => {
            this.loading = false
            _this.$message.error(err.toString())
          })
      }
    })
  }
}
</script>

<style lang="scss" scoped>
.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
</style>